import os
import sys
import json
from PIL import Image

def splitImage(src, rownum, colnum, dstpath):
    print(dstpath)
    if not os.path.exists(dstpath):
        os.makedirs(dstpath)
    img = Image.open(src)
    w, h = img.size
    if rownum <= h and colnum <= w:
        s = os.path.split(src)
        if dstpath == '':
            dstpath = s[0]
        fn = s[1].split('.')
        basename = fn[0]
        ext = fn[-1]
        num = 1
        rowheight = h // rownum
        colwidth = w // colnum
        for r in range(rownum):
            for c in range(colnum):
                box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)
                img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext))
                num = num + 1
    else:
        print('bushu')

if __name__ == '__main__':
    params = sys.argv[1]
    # params = "C:/Users/91941/Desktop/test/20234271445000_1.jpg,C:/Users/91941/Desktop/test/20234271445000_2.jpg"
    print(params)
    if params is None:
        print("dir is empty")
    paths = params.split(",")
    print(len(paths))
    for path in paths:
        dic_name=os.path.dirname(path)+"/"
        all_file_name=os.path.basename(path)
        file_name=all_file_name.split(".")[0]
        splitImage(path, 10, 10, dic_name+file_name)
